
fun performOperation(a: Int, b: Int) {
    // 内部函数
    fun addition(x: Int, y: Int): Int {
        return x + y
    }

    fun demo() {}
    val labda = { x: Int, y: Int -> x + y }
    println(labda(2, 3))
    val result = applyOperation(a, b, ::addition)

    val sum: (Int, Int) -> Int = { a, b ->
        val result = a + b
        println("Calculating sum of $a and $b")
        result // 最后一行自动返回
    }
    val    func1 = fun (x: Int, y: Int ) : Int {
         var result = x + y
        var result2 = x - y
        return result + result2
    }
    fun  func2 (x: Int, y: Int ) : Int {
         var result = x + y
            var result2 = x - y
        return result + result2
    }
    println(func1(2,3))
}

// 传递外部函数给内部函数


// 高阶函数，将外部函数作为参数
fun applyOperation(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
    return operation(a, b)
}
fun  iife1  () {
    val result = fun(): Int {
        val a = 5
        val b = 3
        println("Sum: ${a + b}")
        return a + b
    }() // Immediately invoking the anonymous function

    println("The result is: $result")
}
fun iife2  () {
  var result  = {
        var a = 5
        var b = 3
        println("Sum: ${a + b}")
   a   + b
  }
    println("The result is: $result")
}

fun main() {

   iife2() ;
}

